package net.ds0101.FindTen;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.Log;
import net.ds0101.framework.android.neox.action.NXAction;
import net.ds0101.framework.android.neox.action.NXMove;
import net.ds0101.framework.android.neox.action.NXRepeat;
import net.ds0101.framework.android.neox.core.NXMenuItemActionHandler;
import net.ds0101.framework.android.neox.core.NXNode;
import net.ds0101.framework.android.neox.util.NXEasingCurve;
import net.ds0101.framework.android.neox.util.NXResourceConverter;
import net.ds0101.framework.android.neox.widget.NXBitmapSheet;
import net.ds0101.framework.android.neox.widget.NXLabelBitmap;
import net.ds0101.framework.android.neox.widget.NXMenuItem;
import net.ds0101.framework.android.neox.widget.NXSprite;

public class Tray extends NXMenuItem {
	private float mRadius;
	protected int[] mValuesInside;
	protected GameScene mHolder;
	protected NXAction mAction;
	protected NXSprite mCircle;
	private NXLabelBitmap mNumLabel[];
	private static NXBitmapSheet sTexture;

	public Tray(GameScene holder, Point pos, int radius) {
		super(null);
		/*setTappedHandler(new NXMenuItemActionHandler() {
			@Override
			public void onPressed(NXNode arg0) {
			}

			@Override
			public void onReleased(NXNode arg0) {
			}

			@Override
			public void onTapped(NXNode arg0) {
				if (null != Tray.this.mValuesInside) {
					Tray.this.mHolder.touchResult(GameScene.isTargetValue(Tray.this.mValuesInside) ? true : false);
					//Log.w("[NEOX]", (GameScene.isTargetValue(Tray.this.mValuesInside) ? "true item" : "wrong item") + "Touched.");
				}
			}
		});*/
		setOnClickListener(new OnClickCallback() {
			@Override
			public void onClick(NXNode node) {
				if (null != Tray.this.mValuesInside) {
					Tray.this.mHolder.touchResult(GameScene.isTargetValue(Tray.this.mValuesInside) ? true : false);
				}
			}
		});
		mHolder = holder;
		mRadius = radius;
		if (null == sTexture) {
			sTexture = new NXBitmapSheet(NXResourceConverter.getBitmapFromResource(mHolder, R.drawable.circle));
		}
		mCircle = new NXSprite(sTexture);
		addChild(mCircle);
		mNumLabel = new NXLabelBitmap[GameScene.SEPARATE];
		for (int n = 0; n < mNumLabel.length; ++n) {
			mNumLabel[n] = new NXLabelBitmap("0", "BigNum", 0, 0);
			addChild(mNumLabel[n]);
		}
		position(pos);
		//mAction = new NXRepeat(new NXMove(150, position(), new Point(positionX() + (((int)(Math.random() * 10) & 1) == 0 ? 5 : (-5)), positionY()), NXEasingCurve.Type.CosineCurve), 2);
		mAction = new NXRepeat(new NXMove(150, position(), new Point(positionX() + 6, positionY()), NXEasingCurve.Type.CosineCurve), 2);
		
		/*setEventHandler(new NXEventHandler() {
			@Override
			public boolean onTouchEvent(MotionEvent event) {
				if (null != mValuesInside) {
					mHolder.touchResult(GameScene.isTargetValue(mValuesInside) ? true : false);
					Log.w("[NEOX]", (GameScene.isTargetValue(mValuesInside) ? "true item" : "wrong item") + "Touched.");
				}
				Log.w("[NEOX]", "touched");
				return true;
			}
		});
		*/
	}
	
	public boolean areaContains(int x, int y) {
		if (Math.abs(positionX() - x) < mRadius
				&& Math.abs(positionY() - y) < mRadius) {
			return true;
		}
		return false;
	}
	
	public boolean onTouch(int action, float x, float y) {
		return super.onTouch(action, x, y);
	}
	
	public void position(float x, float y) {
		mCircle.position(x, y);
		mNumLabel[0].position(x, (int) (y - mNumLabel[0].height() / 1.5));
		mNumLabel[1].position(x - (int)(mNumLabel[1].width() / 1.5), y + (int)(mNumLabel[1].height() / 1.5));
		mNumLabel[2].position(x + (int)(mNumLabel[2].width() / 1.5), y + (int)(mNumLabel[2].height() / 1.5));
		super.position(x, y);
		//Log.w("[NEOX]", "postion = " + String.valueOf(x) + ", " + String.valueOf(y));
	}
	
    public void opacity(int alpha) {
        mCircle.opacity(alpha);
        for (NXLabelBitmap label : mNumLabel) {
            label.opacity(alpha);
        }
    }

	public void visible(boolean v) {
		super.visible(v);
		mCircle.visible(v);
		for (NXLabelBitmap label : mNumLabel) {
			label.visible(v);
		}
	}
	
	/*public void draw(Canvas canvas) {
		if (null != mValuesInside) {
			Paint textPaint = new Paint();
			textPaint.setColor(Color.BLACK);
		}
	}*/
	
	public void setValues(int[] values) {
		if (values.length == mNumLabel.length) {
			for (int i = 0; i < mNumLabel.length; ++i) {
				mNumLabel[i].text(String.valueOf(values[i]));
				mValuesInside = values;
			}
		}
	}
	
	public void shaking() {
		mAction.stop();
		runAction(mAction);
	}
	
	public void stopShaking() {
		mAction.stop();
	}
}
